package com.yedean.mall.portal.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.yedean.mall.portal.config.AliyunSMSConfig;
import com.yedean.mall.portal.domain.Sms;
import com.yedean.mall.portal.service.AliyunSmsSenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AliyunSmsSenderServiceImpl implements AliyunSmsSenderService {
    @Autowired
    private AliyunSMSConfig smsConfig;
    @Autowired
    private UmsMemberServiceImpl memberService;
    @Override
    public SendSmsResponse sendSms(String phoneNumbers, String templateParamJson, String templateCode) throws Exception {
        // 封装短信发送对象
        Sms sms = new Sms();
        sms.setPhoneNumbers(phoneNumbers);
        sms.setTemplateParam(templateParamJson);
        sms.setTemplateCode(templateCode);

        // 获取短信发送服务机
        Client client = createClient(smsConfig.getAccessKeyId(), smsConfig.getAccessKeySecret());
        //获取短信请求
        SendSmsRequest smsRequest = new SendSmsRequest()
                .setSignName("阿里云短信测试")
                .setPhoneNumbers(sms.getPhoneNumbers())
                .setTemplateCode(sms.getTemplateCode())
                .setTemplateParam(templateParamJson);
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse smsResponse = client.sendSms(smsRequest);
        System.out.println(smsResponse);

        return smsResponse;
    }
    private static Client createClient(String  accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        config.endpoint="dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

}
